package npu.dce.andcontact1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.database.Cursor;

class Server_Invite_Accept implements Runnable{
    
	private String SERVERIP = "10.0.2.15";
    private int SERVERPORT = 600;

    private UpdatePacket update = new UpdatePacket();
   
    private String strVersion, strTypes, strOwner, strGivenName, strMiddleName, strFamilyName, strGender, strSpinPhone, 
    		strPhone, strSpinEmail, strEmail, strSpinIM, strIM, strSpinAddr, strStreet, strPOBox, strCity, strState, strZipCode,
    		strCountry, strSpinSNS, strSNS, strSpinOrg1, strOrg1, strSpinOrg2, strOrg2, strNotes, strTime; 

    private String GivenName, FamilyName, SpinOrg1;

    private int count = 0;
    private int numRow;
    long packetTime, dbTime, contact_id;;
    boolean update_contact;;
    
    private byte[] packet_photo, buf, receive_data;
    
    private String photoSize;

    private ContactDBAdapter mDbHelper_server;
    private DatagramPacket receive_packet;
    UpdatePacket total_contact;
    
    public Server_Invite_Accept(UpdatePacket total, ContactDBAdapter mDbHelper) {            
            total_contact = total;
            mDbHelper_server = mDbHelper;
    }
 
    @Override
    public void run() {
    	// TODO Auto-generated method stub        
        	try {
        		
        		InetAddress inetadd = InetAddress.getByName(SERVERIP);
        		DatagramSocket socket = new DatagramSocket(SERVERPORT,inetadd);
        		
        		while(true){                	
        			
        			try{
        				        				    				
        				buf = new byte[1920];
        				receive_packet= new DatagramPacket(buf,buf.length);
        				
        				socket.receive(receive_packet);
        				        				
        				receive_data = receive_packet.getData();
        				update = new UpdatePacket(receive_data, 1920);
        				
        				strVersion = update.getVersion();        				
        				strTypes = update.getTypes();  
        				strOwner = update.getOwner();
        				strGivenName = update.getGivenName();        				        				         				
        				strMiddleName = update.getMiddleName();
        				strFamilyName = update.getFamilyName();
        				strGender = update.getGender();
        				strSpinPhone = update.getSpinPhone();
        				strPhone = update.getPhone();
        				strSpinEmail = update.getSpinEmail();
        				strEmail = update.getEmail();
        				strSpinIM = update.getSpinIM();
        				strIM = update.getIM();
        				strSpinAddr = update.getSpinPostalAddr();
        				strStreet = update.getStreet();
        				strPOBox = update.getPOBox();
        				strCity = update.getCity();
        				strState = update.getState();
        				strZipCode = update.getZipCode();
        				strCountry = update.getCountry();
        				strSpinSNS = update.getSpinSNS();
        				strSNS = update.getSNS();
        				strSpinOrg1 = update.getSpinOrg1();
        				strOrg1 = update.getOrg1();
        				strSpinOrg2 = update.getSpinOrg2();
        				strOrg2 = update.getOrg2();		
        				strNotes = update.getNotes();
        				strTime = update.getTime();
        				photoSize = update.getPhotosize();        				       				            			    				
        				packet_photo = update.getPhoto(Integer.parseInt(photoSize));        				        				      				        				        			        							
        				packetTime = Long.parseLong(update.getTime());

        				if(strTypes.compareTo("join") == 0){
        		        
        					try {
        		        	
        						Cursor cur = mDbHelper_server.fetchAllContacts(); 
        		        	
        						numRow = cur.getCount();
        						
        						if(cur != null){
        							if (cur.moveToFirst()){
        		        			
        								count = 0;	
        								do{
        									count++;
        		        				
            		            			GivenName = cur.getString(cur.getColumnIndex(ContactDBAdapter.KEY_GIVENNAME));
            		            			FamilyName = cur.getString(cur.getColumnIndex(ContactDBAdapter.KEY_FAMILYNAME));
            		            			SpinOrg1 = cur.getString(cur.getColumnIndex(ContactDBAdapter.KEY_SPINORG1));

        									if((strGivenName.compareTo(GivenName) ==  0) && (strFamilyName.compareTo(FamilyName) == 0)){ //if givenname and familyname are same
        	            						
        										if((strSpinOrg1.compareTo(SpinOrg1) == 0)){
        											
        											String rowId = cur.getString(cur.getColumnIndexOrThrow(ContactDBAdapter.KEY_ROWID));                                                

        											update_contact = mDbHelper_server.updateContact(Long.parseLong(rowId), strVersion, strTypes, strOwner,strGivenName, strMiddleName, strFamilyName, strGender, strSpinPhone, 
	        						 		 			 		strPhone, strSpinEmail, strEmail, strSpinIM, strIM, strSpinAddr, strStreet, strPOBox, strCity, strState, strZipCode,
	        						 		 			 		strCountry, strSpinSNS, strSNS, strSpinOrg1, strOrg1, strSpinOrg2, strOrg2, strNotes, strTime, packet_photo);               		                    		        						     						
 										        												
        											break;    											 
        										} 
        										else{        										
        											
        											String rowId = cur.getString(cur.getColumnIndexOrThrow(ContactDBAdapter.KEY_ROWID));                                                

        											update_contact = mDbHelper_server.updateContact(Long.parseLong(rowId), strVersion, strTypes, strOwner,strGivenName, strMiddleName, strFamilyName, strGender, strSpinPhone, 
        						 		 			 		strPhone, strSpinEmail, strEmail, strSpinIM, strIM, strSpinAddr, strStreet, strPOBox, strCity, strState, strZipCode,
        						 		 			 		strCountry, strSpinSNS, strSNS, strSpinOrg1, strOrg1, strSpinOrg2, strOrg2, strNotes, strTime, packet_photo);               		                    		        						     						

    												break;
        											
        										}
        										
        									} else{ //different givenname and familyname
          	            													
        										if(numRow == count){
          	            						
        											contact_id = mDbHelper_server.createContact(strVersion, strTypes, strOwner, strGivenName, strMiddleName, strFamilyName, strGender, strSpinPhone, strPhone, 
       	            									 strSpinEmail, strEmail, strSpinIM, strIM, strSpinAddr, strStreet, strPOBox, strCity, strState, 
       	            									 strZipCode, strCountry, strSpinSNS, strSNS, strSpinOrg1, strOrg1, strSpinOrg2, strOrg2, strNotes, strTime, packet_photo);
        											
        											break;
        										} 
        											        										
        									}                    	            				         	  
 
        								}while (cur.moveToNext());        		        			
        							} //if
        						} // if
        						
        						cur.close();         		       	        	
        						
        						} catch (Exception e) {
        							e.printStackTrace();
        						}  
        					} 
        				
        				} catch(IOException e) {
        					// TODO Auto-generated catch block
        					e.printStackTrace();
        				} catch(NullPointerException e) {
        					e.printStackTrace();	
        				} 
        			} //while  
        		
        	} catch (UnknownHostException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	} catch (SocketException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();       
        	} 
        
    } //run    
} //Server_Invite_Accept